package com.demo.my.Controller;

import com.demo.my.Entity.Department;
import com.demo.my.Entity.Employee;
import com.demo.my.Entity.Leave;
import com.demo.my.Service.DepartmentService;
import com.demo.my.Service.LeaveService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;

//高和召
@WebServlet("/addLeave")
public class AddLeaveServlet extends HttpServlet {
    private DepartmentService departmentService=new DepartmentService();
    private LeaveService leaveService=new LeaveService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        HttpSession session = req.getSession();
        Employee employee = (Employee) session.getAttribute("user_info");
        Integer employeeNumber = employee.getEmployeeNumber();
        Integer departmentNumber = employee.getDepartmentNumber();
        try {
            String start = req.getParameter("start");
            String end = req.getParameter("end");
            String days = req.getParameter("days");
            String reason = req.getParameter("reason");
            String type = req.getParameter("type");
            Department department =departmentService.getDepartment(departmentNumber);
            String manager = department.getManager();
            String status="未批准";
            String notes="无";
            Leave leave = new Leave(0, employeeNumber, departmentNumber, start, end, days, type, reason, manager, status, notes);
            leaveService.addLeave(leave);
            req.getRequestDispatcher("/selfLea").forward(req,resp);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
